﻿package idv.etrex.display
{
	import flash.display.Graphics;
	
	/**
	 * GraphicsBat用來作graphics的批次處理。他是一個IGraphics的容器，可透過add和remove方法可以加入和移除IGraphics物件。對GraphicsBat下的graphics指令都會套用到他底下的IGraphics物件身上。
	 * 由於GraphicsBat本身也實作IGraphics介面，所以可以把一個GraphicsBat加入到另一個GraphicsBat底下，作出階層式的樹狀結構。
	 * 注意：當兩個GraphicsBat互相加入對方到自己底下時，會發生無窮遞迴。
	 * @includeExample Demo.txt
	 * @author etrex
	 */
	public class GraphicsBat implements IGraphics
	{
		private var children:Array = new Array();
		
		public function GraphicsBat(){
		}
		
		/**
		 * 加入IGraphics物件，同一個物件加入多次時，只有第一次會成功
		 * @param	ig
		 */
		public function add(ig:IGraphics):void {
			if( children.indexOf(ig) == -1 )
				children.push(ig);
		}

		/**
		 * 移除IGraphics物件
		 * @param	ig
		 */
		public function remove(ig:IGraphics):void {
			var index:int = children.indexOf(ig);
			if (index != -1)
			{
				children[index] = children[children.length - 1];
				children.pop();
			}
		}
		
		
		// graphics function 
		/**
		 * graphics指令
		 */
		public function beginFill(color:uint, alpha:Number = 1.0):void{
			for each(var g:IGraphics in children)
				g.beginFill(color, alpha);
		}
		
		/**
		 * graphics指令
		 */
		public function clear():void {
			for each(var g:IGraphics in children)
				g.clear();
		}
		
		/**
		 * graphics指令
		 */
		public function drawCircle(x:Number, y:Number, radius:Number):void{
			for each(var g:IGraphics in children)
				g.drawCircle(x, y, radius);
		}
		
		/**
		 * graphics指令
		 */
		public function drawEllipse(x:Number, y:Number, width:Number, height:Number):void{
			for each(var g:IGraphics in children)
				g.drawEllipse(x, y, width, height);
		}
		
		/**
		 * graphics指令
		 */
		public function drawRect(x:Number, y:Number, width:Number, height:Number):void{
			for each(var g:IGraphics in children)
				g.drawRect(x, y, width, height);
		}
		
		/**
		 * graphics指令
		 */
		public function drawRoundRect(x:Number, y:Number, width:Number, height:Number, ellipseWidth:Number, ellipseHeight:Number = NaN):void{
			for each(var g:IGraphics in children)
				g.drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight);
		}
		
		/**
		 * graphics指令
		 */
		public function endFill():void {
			for each(var g:IGraphics in children)
				g.endFill();
		}
		
		/**
		 * graphics指令
		 */
		public function lineStyle(thickness:Number = NaN, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void{
			for each(var g:IGraphics in children)
				g.lineStyle(thickness, color, alpha, pixelHinting, scaleMode, caps, joints, miterLimit);
		}
		
		/**
		 * graphics指令
		 */
		public function moveTo(x:Number, y:Number):void {
			for each(var g:IGraphics in children)
				g.moveTo(x, y);
		}
		
		/**
		 * graphics指令
		 */
		public function lineTo(x:Number, y:Number):void{
			for each(var g:IGraphics in children)
				g.lineTo(x, y);
		}
	}
	
}